package com.avic.rong.course.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.bean.show_bean.CultivateBean;
import com.avic.rong.course.activity.CourseDetailActivity;
import com.avic.rong.utils.DateUtils;
import com.avic.rong.utils.DensityUtils;
import com.avic.rong.utils.GlideUtils;
import com.example.minato.minastore.utils.AppUtils;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Chenjiaxin on 2019/8/11 18:19
 * E-Mail Address：16601203965@163.com
 */
public class CourseIndexAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private LayoutInflater inflater;
    private int noLinePosition = 0;

    List<CultivateBean.CourseBean> dataList;

    public static final int TYPE_TITLE = 1;
    public static final int TYPE_CONTENT = 2;

    public void setNoLinePosition(int noLinePosition) {
        this.noLinePosition = noLinePosition;
    }

    public CourseIndexAdapter(Context context,ArrayList<CultivateBean.CourseBean> dataList) {
        this.mContext = context;
        this.inflater = LayoutInflater.from(context);
        this.dataList = dataList;
    }

    public List<CultivateBean.CourseBean> getDataList() {
        return dataList;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {

        RecyclerView.ViewHolder holder = null;
        View view = null;
        if (viewType == TYPE_TITLE) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_course_title, viewGroup, false);
            holder = new TitleViewHolder(view);
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_course_index, viewGroup, false);
            holder = new CourseIndexViewHolder(view);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        CultivateBean.CourseBean dataBean = dataList.get(position);


        if (dataBean.getViewType() == TYPE_TITLE) {
            TitleViewHolder titleViewHolder = (TitleViewHolder) viewHolder;
            titleViewHolder.tv_title.setText(dataBean.getViewName());

        } else {
            CourseIndexViewHolder indexViewHolder = (CourseIndexViewHolder) viewHolder;
            double price = dataBean.getPrice();
            if ( dataBean.getType()== 2) {
                //线下课程
                if (price != 0.0) {
                    indexViewHolder.tv_course_price.setText("¥" + price);
                } else {
                    indexViewHolder.tv_course_price.setText("");
                }

                indexViewHolder.tv_course_total_time.setText("开课时间：" + DateUtils.getFormatShort(dataBean.getStartTime()));
                indexViewHolder.tv_course_time.setText("开课地点：" + dataBean.getCity());//online_default

                GlideUtils.loaderHanldeRoundImage(mContext, AppUtils.COURSE_IMAGE_URL+dataBean.getCover(), indexViewHolder.img_course_logo, R.mipmap.offline_default, DensityUtils.dp2px(mContext, 6));

            } else {
                double secKillPrice = dataBean.getSecKillPrice();
                if (secKillPrice != 0.0) {
                    indexViewHolder.tv_course_price.setText("¥" + secKillPrice);
                } else {
                    if (price != 0.0) {
                        indexViewHolder.tv_course_price.setText("¥" + price);
                    } else {
                        indexViewHolder.tv_course_price.setText("");
                    }
                }
                if (dataBean.getCoursesHourCount().equals("")&&dataBean.getCoursesHourCount()!=null) {
                    indexViewHolder.tv_course_total_time.setText("课时：暂未详细课时" );
                }else {
                indexViewHolder.tv_course_total_time.setText("课时：" + dataBean.getCoursesHourCount().intValue()+"课时");
                }
                if (dataBean.getCoursesTotleTime().equals("")&&dataBean.getCoursesTotleTime()!=null) {
                    indexViewHolder.tv_course_time.setText("时长：暂无详细时长");
                }else {
                indexViewHolder.tv_course_time.setText("时长：" + dataBean.getCoursesTotleTime());
                }
                GlideUtils.loaderHanldeRoundImage(mContext, AppUtils.COURSE_IMAGE_URL+dataBean.getCover(), indexViewHolder.img_course_logo, R.mipmap.online_default, DensityUtils.dp2px(mContext, 6));

            }
            indexViewHolder.tv_course_name.setText(dataBean.getName());
            indexViewHolder.itemView.setOnClickListener(new MyOnClickerListener(dataBean));

            if (position + 1 == noLinePosition) {
                indexViewHolder.line.setVisibility(View.GONE);
            } else {
                indexViewHolder.line.setVisibility(View.VISIBLE);
            }
        }


    }


    @Override
    public int getItemViewType(int position) {
        return dataList.get(position).getViewType();

    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }


    class MyOnClickerListener implements View.OnClickListener {

        CultivateBean.CourseBean dataBean;

        public MyOnClickerListener(CultivateBean.CourseBean dataBean) {
            this.dataBean = dataBean;
        }

        @Override
        public void onClick(View v) {
            StringBuffer sb = new StringBuffer();
            sb.append(dataBean.getType());
            sb.append(dataBean.getId());
            Logger.e("cjx", sb.toString());
            mContext.startActivity(CourseDetailActivity.getIntent(mContext, sb.toString(), AppUtils.COURSE_IMAGE_URL+dataBean.getCover(),dataBean.getName()));
        }
    }


    class TitleViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_title)
        TextView tv_title;

        public TitleViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    class CourseIndexViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_course_logo)
        ImageView img_course_logo;
        @BindView(R.id.tv_course_name)
        TextView tv_course_name;
        @BindView(R.id.tv_course_total_time)
        TextView tv_course_total_time;
        @BindView(R.id.tv_course_time)
        TextView tv_course_time;
        @BindView(R.id.tv_course_price)
        TextView tv_course_price;
        @BindView(R.id.line)
        View line;

        public CourseIndexViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}
